package com.tx.review.thread_.state;

/**
 * yield测试
 *      礼让
 * @author ：tx
 * @version:
 */
public class YieldDemo {

    private static Object obj = new Object();
    /**
     *  白白白-->30
     * 黑黑黑-->3
     * 白白白-->31
     *
     * 1、yield是使当前线程从运行状态转为就绪状态，让此线程和其他线程同时开始竞争
     * 2、不会释放锁
     *
    */
    public static void main(String[] args) {

        Runnable runnable = ()->{
//            synchronized (obj){
                for (int i = 1; i <= 50; i++) {
                    System.out.println(Thread.currentThread().getName() + "-->" + i);
                    if(i == 30){
                        //礼让线程
                        Thread.yield();
                    }
                }
//            }

        };

        new Thread(runnable,"黑黑黑").start();
        new Thread(runnable,"白白白").start();



    }

}
